{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7954e02f",
   "metadata": {},
   "source": [
    "--------\n",
    "- 2022/09/20/week05\n",
    "- python课堂笔记\n",
    "--------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fae6c17a",
   "metadata": {},
   "source": [
    "# 本周内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c853c5a7",
   "metadata": {},
   "source": [
    "> 1.有序的数据结构之列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cafc246",
   "metadata": {},
   "source": [
    "> 2.用户输入"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "117293a8",
   "metadata": {},
   "source": [
    "> 3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a99442c9",
   "metadata": {},
   "source": [
    "## 思考："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f643a992",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_01_name = \"温欣欣\" # 此时，一个变量中有一个名字\n",
    "\n",
    "# 思考: 如果一个变量想存放一个班级的同学的名字，怎么操作？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6ba85023",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jenny,Tony,Gogo\n"
     ]
    }
   ],
   "source": [
    "网一_name_list = \"Jenny,Tony,Gogo\"\n",
    "print(网一_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "25fd371a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jenny'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考：处理数据会不会比较麻烦？\n",
    "网一_name_list[0:5]\n",
    "## # 取一定范围的值，这种方法我们称其为“切片”\n",
    "### 有序性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a0b5524",
   "metadata": {},
   "source": [
    "#### 思考：如何知道这个名单有多少个人名？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "64dfdfdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(网一_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea2a87b6",
   "metadata": {},
   "source": [
    "## 1.引入列表的概念"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dad5e4af",
   "metadata": {},
   "source": [
    "> list 可以放数值和字符，但是要么通常一起放数值，要么一起放字符串\n",
    "\n",
    ">> 一些通用的的有序数据结构的方法：\n",
    ">>> 1. 取值：通过索引值取出对应的内容\n",
    ">>> 2. 切片：通过索引值区间\n",
    ">>> 3. in 可以查看对象是否存在"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fa5e914",
   "metadata": {},
   "source": [
    "> 列表的一些方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f534c39d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b27a2220",
   "metadata": {},
   "source": [
    "# 1.1列表的运算法则(operation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "936a96a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['jenny', 'tony', 'gogo'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网一_name_list = [\"jenny\",\"tony\",\"gogo\"]\n",
    "print(网一_name_list,type(网一_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a2487dac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jenny'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如何取出 网一_name_list 中第一位同学的名字\n",
    "网一_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "11f13ef6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如何查看 网一_name_list 有多少位学生\n",
    "len(网一_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c0fb4d9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网一:  jenny\n",
      "网一:  tony\n",
      "网一:  gogo\n"
     ]
    }
   ],
   "source": [
    "for i in 网一_name_list:\n",
    "    print(\"网一: \",i)  # i为index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "02b5119a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 可以查询数据是否存在\n",
    "\"jenny\" in 网一_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e01e441d",
   "metadata": {},
   "source": [
    "### 关于运算法则(in  +  切片 取值len(s)s.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0c640f55",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list_转专业 = [\"Xiaoming\",\"Lihua\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eb3c4012",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', 'gogo', 'Xiaoming', 'Lihua']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list + 网一_name_list_转专业"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f20a1d62",
   "metadata": {},
   "source": [
    "# 2.2 列表的方法(Function) 对列表数据进行操作\n",
    "> 1. 增\n",
    "> 2. 删\n",
    "> 3. 改\n",
    "> 4. 查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "688cd33f",
   "metadata": {},
   "source": [
    "### append 增加，增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "71ebc10d",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list = [\"jenny\",\"tony\",\"gogo\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "30c24d85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', 'gogo']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ab15ca51",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.append(\"Jacky\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a7ec1f88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', 'gogo', 'Jacky']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list  ## 变量名称没有变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cebe00e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 再次增加一个人名，会增加在末尾。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "642e3d3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.append(\"Jackson\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2fa5f8ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', 'gogo', '迪丽热巴', 'Jacky', 'Jackson']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fe59b07",
   "metadata": {},
   "outputs": [],
   "source": [
    "### insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b24547fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c921aba8",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.insert(3,\"迪丽热巴\") ## object要双引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "adf3eb7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', 'gogo', '迪丽热巴', 'Jacky', 'Jackson']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e60c6dc7",
   "metadata": {},
   "source": [
    "### remove 移除某元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d78d6cea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b10257cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.remove(\"Jackson\") ## 单引号双引号都可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "23c999ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', 'gogo', '迪丽热巴', 'Jacky']"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "54fe9a80",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [48]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43m网一_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mJackson\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "网一_name_list.remove(\"Jackson\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1067c2e0",
   "metadata": {},
   "source": [
    "### remove 经常和  in  一起使用，可用来检查元素是否存在于变量之中，然后再remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "bf105ed1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', 'gogo', '迪丽热巴', 'Jacky']"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"Jackson\" in 网一_name_list:\n",
    "    网一_name_list.remove(\"Jackson\")\n",
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9a938a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "### pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "42394a94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "883c46f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', 'gogo', '迪丽热巴', 'Jacky']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "b06a3a05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jacky'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.pop() ##"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "8c770ca3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gogo'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "fe4c22e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jenny', 'tony', '迪丽热巴']"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcd34f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践： 移除19的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "e76c56cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,20,23,21,16,17]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "de15d069",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "42096880",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 20, 23, 21, 16, 17]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55ed999a",
   "metadata": {},
   "source": [
    "### 思路1：remove(value),先决条件，知道值出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "ad42b0da",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.remove(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "f413ed09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 20, 23, 21, 16, 17]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "a1e3358c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 20, 23, 21, 16, 17]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7493ee27",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,20,23,21,16,17]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "edf990e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 20, 23, 21, 16, 17]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1b48622",
   "metadata": {},
   "source": [
    "### 思路2：pop(index),先决条件，知道值出现的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "e491fcb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 20, 23, 21, 16, 17]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:  ## 有两个等于号\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4dd7347",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c81a59a3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
